//
//  CountDownView.m
//  phmall
//
//  Created by admin on 2017/9/24.
//  Copyright © 2017年 admin. All rights reserved.
//

#import "CountDownView.h"
@interface CountDownView (){
    UIView *line;
}
@property(nonatomic,strong)NSTimer *timer;
@end
@implementation CountDownView

-(instancetype)init{
    if (self=[super init]) {
        
      
        self.img=[[UIImageView alloc]init];
        self.img.image=[UIImage imageNamed:@"秒杀时钟"];
        [self addSubview:self.img];
        
        self.merchant=[UILabel new];
        [self addSubview:self.merchant];
        self.merchant.text=Localized(@"限时秒杀");
        self.merchant.textColor=[UIColor orangeColor];

        self.hourLabel=[UILabel new];
        [self  addSubview:self.hourLabel];
        self.hourLabel.backgroundColor=[UIColor orangeColor];
        self.hourLabel.text=@"00";
        self.hourLabel.layer.cornerRadius=3;
        self.hourLabel.layer.masksToBounds=YES;
        self.hourLabel.font=[UIFont systemFontOfSize:13];
        self.hourLabel.textColor=[UIColor whiteColor];
        self.hourLabel.textAlignment=NSTextAlignmentCenter;
      
        
        self.colon=[UILabel new];
        self.colon.text=@":";
        self.colon.font=[UIFont systemFontOfSize:13];
        self.colon.textColor=[UIColor orangeColor];
        self.colon.textAlignment=NSTextAlignmentCenter;
        [self addSubview:self.colon];
       
        
        self.minuteLabel=[UILabel new];
        [self  addSubview:self.minuteLabel];
        self.minuteLabel.backgroundColor=[UIColor orangeColor];
        self.minuteLabel.text=@"00";
        self.minuteLabel.layer.cornerRadius=3;
        self.minuteLabel.layer.masksToBounds=YES;
        self.minuteLabel.font=[UIFont systemFontOfSize:13];
        self.minuteLabel.textAlignment=NSTextAlignmentCenter;
        self.minuteLabel.textColor=[UIColor whiteColor];
        
        self.colon2=[UILabel new];
        self.colon2.text=@":";
        self.colon2.font=[UIFont systemFontOfSize:13];
        self.colon2.textColor=[UIColor orangeColor];
        self.colon2.textAlignment=NSTextAlignmentCenter;
        [self addSubview:self.colon2];
        
        self.secondLabel=[UILabel new];
        [self  addSubview:self.secondLabel];
        self.secondLabel.backgroundColor=[UIColor orangeColor];
        self.secondLabel.text=@"00";
        self.secondLabel.layer.cornerRadius=3;
        self.secondLabel.layer.masksToBounds=YES;
        self.secondLabel.font=[UIFont systemFontOfSize:13];
        self.secondLabel.textAlignment=NSTextAlignmentCenter;
        self.secondLabel.textColor=[UIColor whiteColor];
        
        
        self.but=[UIButton buttonWithType:UIButtonTypeCustom];
        [self addSubview:self.but];
       
        //        [self time];
        [self.but setTitle:[NSString stringWithFormat:@"%@>",Localized(@"更多")] forState:UIControlStateNormal];
        [self.but setTitleColor:[UIColor orangeColor] forState:UIControlStateNormal];
        self.but.titleLabel.font=[UIFont systemFontOfSize:12];
        [self.but setImage:[UIImage imageNamed:[NSString stringWithFormat:@"back"]] forState:UIControlStateNormal];
   
        [self.but addTarget:self action:@selector(moreAction) forControlEvents:UIControlEventTouchUpInside];
      
        
        
        line=[[UIView alloc]init];
        line.backgroundColor =[UIColor groupTableViewBackgroundColor];
        [self addSubview:line];
        
        NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
        //指定时间显示样式: HH表示24小时制 hh表示12小时制
        [formatter setDateFormat:@"YYYY-MM-dd HH:mm:ss"];
        NSLog(@"%@",[self getTomorrowTime]);
        NSDate *lastDate = [formatter dateFromString:[self getTomorrowTime]];
        //以 1970/01/01 GMT为基准，得到lastDate的时间戳
        int endStamp = [lastDate timeIntervalSince1970];
        int firstStamp = [self getCurrentTimestamp].doubleValue;
        self.time=endStamp-firstStamp;
        
        [self startime];
   
    }
    return self;
}

-(void)moreAction{
    if (self.delegate && [self.delegate respondsToSelector:@selector(moreAction)]) {
        [self.delegate moreAction];
    }
}
-(void)layoutSubviews{
    [super layoutSubviews];
    CGSize superSize = self.frame.size;
    
    if (CGSizeEqualToSize(superSize, CGSizeZero)) {
        return;
    }
    [self.img mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self).offset(10);
        make.left.equalTo(self).offset(5);
//        make.width.height.offset(20);
    }];
    [self.merchant mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(self.img);
        make.left.equalTo(self.img.mas_right).with.offset(5);
    }];
    [self.hourLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(self);
        make.width.mas_equalTo(20);
        make.left.equalTo(self.merchant.mas_right).with.offset(5);
    }];
    [self.colon mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(self);
        make.left.equalTo(self.hourLabel.mas_right).with.offset(3);
    }];
    [self.minuteLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(self);
        make.width.mas_equalTo(20);
        make.left.equalTo(self.colon.mas_right).with.offset(3);
    }];
    [self.colon2 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(self);
        make.left.equalTo(self.minuteLabel.mas_right).with.offset(3);
    }];
    [self.secondLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(self);
        make.width.mas_equalTo(20);
        make.left.equalTo(self.colon2.mas_right).with.offset(3);
    }];
    [self.but mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(self);
        make.height.mas_equalTo(20);
        make.right.equalTo(self.mas_right).with.offset(-10);
    }];
    self.but.contentHorizontalAlignment = UIControlContentHorizontalAlignmentCenter;//使图片和文字水平居中显示
    [self.but layoutButtonWithEdgeInsetsStyle:MKButtonEdgeInsetsStyleRight
                              imageTitleSpace:0];
    
    [line mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.left.right.equalTo(self);
        make.height.offset(1);
    }];
}
-(void)countTime{
    if(time<=0){ //倒计时结束，关闭
        //取消定时器
        [self.timer invalidate];
        self.timer = nil;
    }else{
        int days = (int)(self.time/(3600*24));
        if (days==0) {

        }
        int hours = (int)(self.time-days*24*3600)/3600;
        int minute = (int)(self.time-days*24*3600-hours*3600)/60;
        int second = (int)self.time-days*24*3600-hours*3600-minute*60;

        dispatch_async(dispatch_get_main_queue(), ^{
            if (days==0) {

            }else{

            }
            if (hours<10) {
                self.hourLabel.text = [NSString stringWithFormat:@"0%d",hours];
            }else{
                self.hourLabel.text = [NSString stringWithFormat:@"%d",hours];
            }
            if (minute<10) {
                self.minuteLabel.text = [NSString stringWithFormat:@"0%d",minute];
            }else{
                self.minuteLabel.text = [NSString stringWithFormat:@"%d",minute];
            }
            if (second<10) {
                self.secondLabel.text = [NSString stringWithFormat:@"0%d",second];
            }else{
                self.secondLabel.text = [NSString stringWithFormat:@"%d",second];
            }

        });
        self.time=self.time-1;
    }
}
-(void)startime{
    self.timer=[NSTimer timerWithTimeInterval:1 target:self selector:@selector(countTime) userInfo:nil repeats:YES];
    [[NSRunLoop mainRunLoop] addTimer:self.timer forMode:NSRunLoopCommonModes];
}
- (NSString *)getCurrentTime{
    NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
    [formatter setDateFormat:@"yyyy-MM-dd"];
    NSString *dateTime = [formatter stringFromDate:[NSDate date]];
    return dateTime;
}
- (NSDate *)dateFromString:(NSString *)dateString{
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    [dateFormatter setDateFormat: @"yyyy-MM-dd"];
    NSDate *destDate= [dateFormatter dateFromString:dateString];
    return destDate;
}
//获取当前时间的时间戳
-(NSString*)getCurrentTimestamp{
    NSDate* date = [NSDate dateWithTimeIntervalSinceNow:0];
    NSTimeInterval a=[date timeIntervalSince1970]; // *1000 是精确到毫秒，不乘就是精确到秒
    NSString *timeString = [NSString stringWithFormat:@"%.0f", a]; //转为字符型
    return timeString;
}
- (NSString *)getTomorrowTime{
    NSCalendar *gregorian = [[NSCalendar alloc] initWithCalendarIdentifier:NSCalendarIdentifierGregorian];
    NSDateComponents *components = [gregorian components:NSCalendarUnitWeekday | NSCalendarUnitYear | NSCalendarUnitMonth | NSCalendarUnitDay fromDate: [self dateFromString:[self getCurrentTime]]];
    [components setDay:([components day]+1)];
    NSDate *beginningOfWeek = [gregorian dateFromComponents:components];
    NSDateFormatter *dateday = [[NSDateFormatter alloc] init];
    [dateday setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
    return [dateday stringFromDate:beginningOfWeek];
}
@end
